package com.ficus.road.maintain.dao;

import com.ficus.road.maintain.core.dao.BaseMongoDao;
import com.ficus.road.maintain.core.model.zh.ComplainInfo;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * 投诉接入
 */
@Component
public class ComplainDao extends BaseMongoDao<ComplainInfo, String> {
    @Override
    public String getCollectionName() {
        return "complain_info";
    }


    public List<ComplainInfo> get(int pageNumber, int pageSize){

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime oneYearAgo = now.minusYears(1);
        // 转换为Date类型
        Date oneYearAgoDate = Date.from(oneYearAgo.atZone(ZoneId.systemDefault()).toInstant());
        // 构建查询条件
        Criteria time = Criteria.where("reportTime").gt(oneYearAgoDate).lt(new Date());

        Query query = new Query(time);
        // 创建分页请求对象
        PageRequest pageRequest = PageRequest.of(pageNumber, pageSize);
        // 设置分页参数
        query.with(pageRequest);
        return this.findByQuery(query);

    }

    public Long getRoadCorrelationCount() {

        // 构建查询条件
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime oneYearAgo = now.minusYears(1);
        // 转换为Date类型
        Date oneYearAgoDate = Date.from(oneYearAgo.atZone(ZoneId.systemDefault()).toInstant());
        // 构建查询条件
        Criteria time = Criteria.where("reportTime").gt(oneYearAgoDate).lt(new Date());

        Query query = new Query(time);

        return this.count(query);
    }
}
